const app = getApp()
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"", //订单id
    order_message:"",//订单信息
    staff_phone:"",//负责人电话
    phone:"", //用户手机号
    order_refund:"",//退款原因
    is_customer:"",//是否是从客户管理进来的
    roleCode:"",//1是分总 2员工 3财务
    is_show:false, //弹窗
    deposit:"",//定金金额
    remarks:"",//备注
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      is_customer:options.is_customer,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getLevelData();  //身份获取，1分总 2员工 3财务
    this.getOrderData();
    this.getRemarks(); //获取备注
  },
  //获取角色
  getLevelDataModel:new BaseModel({
    url:"getLevel",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getLevelData(){
    this.getLevelDataModel
        .fetch({  })
        .then(res => {
          //成功 (1分总2普通员工)
          app.globalData.role=res.data;
          wx.setStorageSync('roleCode',res.data)
          this.setData({
            roleCode:res.data
          })
        })
        .catch(res => {
        })
  },
  /*订单数据*/
  getOrderDataModel: new BaseModel({
    url: 'detailsOrder',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getOrderData() {
    var that = this;
    this.getOrderDataModel
        .fetch({
          orderid:this.data.id
        })
        .then(res => {
          that.setData({
            order_message:res.data,
            phone:res.data.user.phone,
            staff_phone:res.data.staff.staff_phone,
          })
        })
        .catch(res => {

        })
  },
  /*提交表单信息*/
  formSubmit: function(e) {
    if(!(/^\d+$/.test(e.detail.value.deposit))){
      wx.showToast({
        title: '定金必须为数字哦',
        icon: 'none'
      })
      return false;
    }
    this.setData({
      deposit:e.detail.value.deposit
    })
    this.getDepositData();
  },
  //定金提交
  getDepositDataModel:new BaseModel({
    url:"employees/depositOrder",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getDepositData(){
    this.getDepositDataModel
        .fetch({
          orderid:this.data.id,
          deposit:this.data.deposit,
        })
        .then(res => {
          wx.navigateBack({
            delta: 1
          })
        })
        .catch(res => {
        })
  },
  /*关闭弹窗*/
  closeModel:function(){
    this.setData({
      is_show:false
    })
  },
  /*显示定金收款*/
  sure_deposit:function(){
    this.setData({
      is_show:true
    })
  },
  /*禁止页面滚动*/
  preventTouchMove:function(e) {

  },
  //拨打客户电话
  tel:function(){
    wx.makePhoneCall({
      phoneNumber: this.data.phone // 仅为示例，并非真实的电话号码
    })
  },
  //拨打负责人电话
  tel_staff:function(e){
    wx.makePhoneCall({
      phoneNumber: this.data.staff_phone // 仅为示例，并非真实的电话号码
    })
  },
  /*点击用户信息 查看用户*/
  clickUserMes:function(){
    wx.navigateTo({
      url: '/pages/customer/customer_details?id='+this.data.order_message.user.id+'&roleCode='+this.data.roleCode
    })
  },
  /*备注，textarea失焦事件*/
  textareablur:function(e){
    this.setData({
      remarks:e.detail.value
    })
    this.getAddRemarks();
  },
  /*修改备注*/
  getAddRemarksModel: new BaseModel({
    url: 'employees/addRemark',
    method: "get",
    needAjaxLoading: true,
    loadingText: "玩命加载中"
  }),
  getAddRemarks(){
    this.getAddRemarksModel
        .fetch({
          orderid: this.data.id, //订单id
          remarks:this.data.remarks, //备注
        })
        .then(res => {

        })
        .catch(res => {
          //失败
        })
  },
  /*获取备注*/
  getRemarksModel: new BaseModel({
    url: 'employees/getsRemark',
    method: "get",
    needAjaxLoading: true,
    loadingText: "玩命加载中"
  }),
  getRemarks(){
    this.getRemarksModel
        .fetch({
          orderid: this.data.id, //订单id
        })
        .then(res => {
            this.setData({
              remarks:res.data.remarks
            })
        })
        .catch(res => {
          //失败
        })
  },
  /*拒绝退款*/
  noRefund:function(){
    var that = this;
    wx.showModal({
      title: '确定拒绝退款吗？',
      content: '提示',
      success(res) {
        if (res.confirm) {
          that.getUsernoRefundData();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  getUsernoRefundModel:new BaseModel({
    url: "refuseTicket",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getUsernoRefundData(){
    this.getUsernoRefundModel
        .fetch({
          orderid:this.data.id
        })
        .then(res => {
          //成功
          this.getOrderData();
        })
        .catch(res => {
          //失败
          wx.showToast({
            title: '请求失败',
            icon: 'none'
          })
        })
  },

  /*同意退款*/
  agreeRefund:function(){
    var that = this;
    wx.showModal({
      title: '确定同意退款吗？',
      content: '提示',
      success(res) {
        if (res.confirm) {
          that.getUserAgreeRefundData();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  getUserAgreeRefundModel:new BaseModel({
    url: "returnTicket",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getUserAgreeRefundData(){
    this.getUserAgreeRefundModel
        .fetch({
          orderid:this.data.id
        })
        .then(res => {
          //成功
          this.getOrderData();
        })
        .catch(res => {
          //失败
          wx.showToast({
            title: '请求失败',
            icon: 'none'
          })
        })
  },

  /*财务确认收款*/
  sure_receipt:function(){
    var that = this;
    wx.showModal({
      title: '确认收款吗？',
      content: '请确定收款到账后再进行操作',
      success(res) {
        if (res.confirm) {
          that.getSureReceiptData();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  getSureReceiptModel:new BaseModel({
    url: "employees/confirmOrder",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getSureReceiptData(){
    this.getSureReceiptModel
        .fetch({
          orderid:this.data.id
        })
        .then(res => {
          //成功
          /*wx.switchTab({
            url:"/pages/index/index"
          })*/
          wx.navigateBack({
            delta: 1
          })
        })
        .catch(res => {
          //失败
          wx.showToast({
            title: '请求失败',
            icon: 'none'
          })
        })
  },

  /*财务确认退款*/
  sure_refund:function(){
    var that = this;
    wx.showModal({
      title: '确认退款吗？',
      content: '同意退款后请及时与用户联系退款事宜',
      success(res) {
        if (res.confirm) {
          that.getSureRefundData();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  getSureRefundModel:new BaseModel({
    url: "employees/refundOrder",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getSureRefundData(){
    this.getSureRefundModel
        .fetch({
          orderid:this.data.id
        })
        .then(res => {
          //成功
          wx.switchTab({
            url:"/pages/index/index"
          })
        })
        .catch(res => {
          //失败
          wx.showToast({
            title: '请求失败',
            icon: 'none'
          })
        })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getLevelData();  //身份获取，1分总 2员工 3财务
    this.getOrderData();
    this.getRemarks(); //获取备注
    wx.stopPullDownRefresh(); //停止下拉
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


})